package com.erebus.centroller;


import com.erebus.model.dto.TokenUser;
import com.erebus.model.dto.User;
import com.erebus.service.YanQiService;
import com.erebus.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.ParseException;

@RestController
public class YanQiController {

    @Autowired
    private YanQiService yanQiService;
    @Autowired
    private RedisTemplate redisTemplate;

    // test
    @GetMapping("/test1")
    public String test1() throws IOException {
        return "test1";
    }


    @GetMapping("/getBian")
    public int getBian(HttpServletRequest request) throws ParseException {
        TokenUser tokenUser = null;
        String authorizationHeader = request.getHeader("Authorization");
        // 检查是否包含 Bearer Token
        if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
            String token = authorizationHeader.substring(7); // 去掉 "Bearer " 前缀
            tokenUser = JwtUtils.parseToken(token);
        }


        return yanQiService.getBian(tokenUser.getUsername());
    }

    @GetMapping("/getYao")
    public int getYao(HttpServletRequest request) throws ParseException {
        TokenUser tokenUser = null;
        String authorizationHeader = request.getHeader("Authorization");
        // 检查是否包含 Bearer Token
        if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
            String token = authorizationHeader.substring(7); // 去掉 "Bearer " 前缀
            tokenUser = JwtUtils.parseToken(token);
        }
        return yanQiService.getYao(tokenUser.getUsername());
    }

    @GetMapping("/getImg")
    public ResponseEntity<byte[]> getIma(HttpServletRequest request) throws IOException, ParseException {
        TokenUser tokenUser = null;
        String authorizationHeader = request.getHeader("Authorization");
        // 检查是否包含 Bearer Token
        if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
            String token = authorizationHeader.substring(7); // 去掉 "Bearer " 前缀
            tokenUser = JwtUtils.parseToken(token);
        }
        return yanQiService.getImg(tokenUser.getUsername());
    }
    @GetMapping("/getImgs/{codeStr}")
    public ResponseEntity<byte[]> getImas(@PathVariable(value = "codeStr") String codeStr) throws IOException {
        return yanQiService.getImgs(codeStr);
    }

    @GetMapping("/getText")
    public String getText(HttpServletRequest request) throws  ParseException {
        TokenUser tokenUser = null;
        String authorizationHeader = request.getHeader("Authorization");
        // 检查是否包含 Bearer Token
        if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
            String token = authorizationHeader.substring(7); // 去掉 "Bearer " 前缀
            tokenUser = JwtUtils.parseToken(token);
        }
        return yanQiService.getText(tokenUser.getUsername());
    }

    @GetMapping("/getTexts/{codeStr}")
    public String getTexts(@PathVariable(value = "codeStr") String codeStr) {
        return yanQiService.getTexts(codeStr);
    }
}
